package cn.stylefeng.guns.core;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述：搜索条件参数封装工具类
 * 作者：chenhuiyan
 * 时间：2020/04/14 21:40
 */
public class ParamUtil {


    private static Map<String, Object> params = new HashMap<>(); //存放搜索条件参数


    /**
     * 获取一个request对象的方法
     *
     * @return HttpServletRequest
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return (HttpServletRequest) attrs.getRequest();
    }

    /**
     * 获取参数
     * @return
     */
    public static Map<String, Object> getParams(){

        HttpServletRequest request = getRequest();

        //键值对格式搜索条件参数处理
        Map<String, String[]> pa = request.getParameterMap();
        String[] arr = null;
        for(Map.Entry<String, String[]> map: pa.entrySet()){
            arr = map.getValue();
            params.put(map.getKey(), arr[0]);
        }
        return params;
    }

}
